package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
@RestController
@RequestMapping("/mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/delayed")
    public Result delayed(){

        //时间格式化对象
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //发送消息
        rabbitService.sendMessaged(
                DelayedMqConfig.exchange_delay,
                DelayedMqConfig.routing_delay,
                "我是延迟插件的消息",
                10
        );
        System.out.println("消息发送的时间：\t"+format.format(new Date()));


        return Result.ok();
    }

    /**
     延迟消息-死信队列

    */
    @GetMapping("/deadLetter")
    public Result deadLetter(){

        //时间格式化对象
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        rabbitTemplate.convertAndSend(

                DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1,
                "我是延迟消息，我来了。。。"

        );
        System.out.println("发送的时间为：\t"+format.format(new Date()));

        return Result.ok("消息已发送");
    }

    /**
     消息确认模式

    */
    @GetMapping("/confirm")
    public Result sendConfirm(){

        rabbitService.sendMessaged(
                "exchange.confirm",
                "routingKey.confirm",
                "我来了，我的女神……等我有钱了，找你。。。别走！"

        );

        System.out.println("我是屌丝，我来了");

        return Result.ok("消息发送成功！");
    }


}
